Repository navigation
Conversation
|
Here is the summary of changes. You are about to add 1 region tag.
This comment is generated by snippet-bot.
|
There was a problem hiding this comment.
Code Review
This pull request introduces a new module to register a remote Vertex AI text-embedding model in a Spanner schema, along with its corresponding unit and integration tests. The feedback suggests re-raising exceptions in the create_embedding_model function instead of only printing them to stderr, which ensures that failures are properly propagated to programmatic callers and CLI executions. Additionally, the unit tests should be updated to expect these exceptions.
| try: | ||
| operation = client.update_database_ddl(request=request) | ||
| print(f"Waiting for remote model '{model_name}' registration...") | ||
| operation.result(timeout=300) | ||
| except exceptions.AlreadyExists as err: | ||
| print( | ||
| f"error: model '{model_name}' already exists on database " | ||
| f"'{database_id}' in instance '{instance_id}' and location " | ||
| f"'{location_id}' for project '{project_id}': {err.message}", | ||
| file=sys.stderr, | ||
| ) | ||
| except exceptions.GoogleAPICallError as err: | ||
| print( | ||
| f"error: failed to register model '{model_name}' on database " | ||
| f"'{database_id}' in instance '{instance_id}' and location " | ||
| f"'{location_id}' for project '{project_id}': {err.message}", | ||
| file=sys.stderr, | ||
| ) |
There was a problem hiding this comment.
Catching the exceptions and only printing them to stderr causes the function to return normally (None). This means that any programmatic caller will not know that the operation failed, and running this file as a CLI script will exit with status 0 (success) even on failure. It is better to re-raise the exceptions so that failures are propagated correctly.
| try: | |
| operation = client.update_database_ddl(request=request) | |
| print(f"Waiting for remote model '{model_name}' registration...") | |
| operation.result(timeout=300) | |
| except exceptions.AlreadyExists as err: | |
| print( | |
| f"error: model '{model_name}' already exists on database " | |
| f"'{database_id}' in instance '{instance_id}' and location " | |
| f"'{location_id}' for project '{project_id}': {err.message}", | |
| file=sys.stderr, | |
| ) | |
| except exceptions.GoogleAPICallError as err: | |
| print( | |
| f"error: failed to register model '{model_name}' on database " | |
| f"'{database_id}' in instance '{instance_id}' and location " | |
| f"'{location_id}' for project '{project_id}': {err.message}", | |
| file=sys.stderr, | |
| ) | |
| try: | |
| operation = client.update_database_ddl(request=request) | |
| print(f"Waiting for remote model '{model_name}' registration...") | |
| operation.result(timeout=300) | |
| except exceptions.AlreadyExists as err: | |
| print( | |
| f"error: model '{model_name}' already exists on database " | |
| f"'{database_id}' in instance '{instance_id}' and location " | |
| f"'{location_id}' for project '{project_id}': {err.message}", | |
| file=sys.stderr, | |
| ) | |
| raise | |
| except exceptions.GoogleAPICallError as err: | |
| print( | |
| f"error: failed to register model '{model_name}' on database " | |
| f"'{database_id}' in instance '{instance_id}' and location " | |
| f"'{location_id}' for project '{project_id}': {err.message}", | |
| file=sys.stderr, | |
| ) | |
| raise |
There was a problem hiding this comment.
Thanks for the feedback! Standalone documentation snippets here intentionally catch google.api_core.exceptions inside the sample function and print actionable diagnostic messages to stderr so readers can see concrete error handling directly in context, so we are keeping the try/except block.
| def test_create_embedding_model_errors( | ||
| mock_client_class: mock.MagicMock, | ||
| capsys: pytest.CaptureFixture[str], | ||
| error: Exception, | ||
| expected_prefix: str, | ||
| ) -> None: | ||
| client = mock_client_class.return_value | ||
| client.database_path.return_value = DATABASE_PATH | ||
| client.update_database_ddl.side_effect = error | ||
|
|
||
| create_embedding_model.create_embedding_model( | ||
| PROJECT_ID, LOCATION_ID, INSTANCE_ID, DATABASE_ID | ||
| ) | ||
|
|
||
| _out, err = capsys.readouterr() | ||
| assert err.startswith(expected_prefix) |
There was a problem hiding this comment.
Update the test to expect the exception to be raised, aligning with the propagated exceptions in the main function.
| def test_create_embedding_model_errors( | |
| mock_client_class: mock.MagicMock, | |
| capsys: pytest.CaptureFixture[str], | |
| error: Exception, | |
| expected_prefix: str, | |
| ) -> None: | |
| client = mock_client_class.return_value | |
| client.database_path.return_value = DATABASE_PATH | |
| client.update_database_ddl.side_effect = error | |
| create_embedding_model.create_embedding_model( | |
| PROJECT_ID, LOCATION_ID, INSTANCE_ID, DATABASE_ID | |
| ) | |
| _out, err = capsys.readouterr() | |
| assert err.startswith(expected_prefix) | |
| def test_create_embedding_model_errors( | |
| mock_client_class: mock.MagicMock, | |
| capsys: pytest.CaptureFixture[str], | |
| error: Exception, | |
| expected_prefix: str, | |
| ) -> None: | |
| client = mock_client_class.return_value | |
| client.database_path.return_value = DATABASE_PATH | |
| client.update_database_ddl.side_effect = error | |
| with pytest.raises(type(error)): | |
| create_embedding_model.create_embedding_model( | |
| PROJECT_ID, LOCATION_ID, INSTANCE_ID, DATABASE_ID | |
| ) | |
| _out, err = capsys.readouterr() | |
| assert err.startswith(expected_prefix) |
There was a problem hiding this comment.
Thanks! Since the sample keeps its explicit exception handling and stderr reporting, this test keeps its capsys stderr assertions.
Registering a remote Vertex AI embedding endpoint in Cloud Spanner requires matching the model's exact INPUT and OUTPUT struct schemas in database DDL.
Prints short resource identifiers instead of full project-prefixed resource paths and omits project_id from diagnostic messages.
Problem: the sample waits on a long-running operation with a client-side timeout. When that wait expires the client raises TimeoutError, which is not an API call error, so the sample ended with an unhandled traceback. Solution: catch TimeoutError and print a clear message to stderr noting that only the wait timed out and the operation may still be running (the timeout does not cancel it). Add a unit test covering the new handler.
7b8f45d to
1e9c7d1
Compare
Problem: reviewers of this sample set asked for stricter and clearer tests and for a single source of truth for dependencies. Mocks were created without a spec, so a renamed client argument would pass silently; the mocked wait timeout was raised from the API call instead of from the operation's result; live tests did not say which environment variables they need; and some tests carried leftover or repeated lines. Package dependencies were pinned in both pyproject.toml and requirements.txt. Solution: where applicable to the files below, patch client classes with autospec=True, raise the mocked TimeoutError from operation.result(), document the required environment variables above each live test and skip when one is missing, remove unused or duplicated test lines, and join string literals that the formatter had left split on one line. For package scaffolding, read dependencies from requirements.txt and update the README install command to match. Files updated: - spanner/search_and_ai/tests/create_embedding_model_test.py
Description
Calling Vertex AI text embedding models from Cloud Spanner SQL (
ML.PREDICT) first requires registering the remote publisher endpoint with aCREATE MODELDDL statement that declares the model'sINPUT(content) andOUTPUT(embeddings STRUCT<values ARRAY<FLOAT32>>) signature.Adds
spanner_create_embedding_modeldemonstrating asynchronousCREATE MODELregistration fortext-embedding-005.Checklist
Testing
Compliance & Style
spanner/, which the default repository owners cover.Post-Approval Actions